﻿using System;
using System.Data;
using System.Data.SqlClient;
using PPI.Data;
using PPI.Entities.Distribution;

namespace PPIDataAccess.Distribution
{
    public class DMeterNote
    {
        public static DataSet Select(string connectionKey)
        {
            SqlDataAdapter da = null;

            try
            {
                da = new SqlDataAdapter("up_tb_Vendor_Distribution_NotesXMeter_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception exc)
            {
                throw new Exception("Error executing up_tb_Vendor_Distribution_NotesXMeter_SelectAll", exc);
            }
            finally
            {
                if (da != null)
                    da.SelectCommand.Connection.Close();
            }
        }

        public static int Insert(ENote note, Int32 meterId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_NotesXMeter_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Note

                if (note.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_NoteFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_NoteFk", ParameterDirection.Input, true, Convert.ToInt32(note.Key)));

                #endregion

                #region Meter Id

                if (meterId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_MeterFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_MeterFk", ParameterDirection.Input, true, meterId));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionMeterNote_MeterNoteId", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionMeterNote_MeterNoteId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();

                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }


        public static DataSet SelectAll(string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Distribution_MeterNotes_SelectAll", PPI.Data.DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Distribution_MeterNotes_SelectAll", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }
    }
}
